package cn.itcast.work.dao.user;


import cn.itcast.work.pojo.CUser;
import cn.itcast.work.pojo.InfoText;
import cn.itcast.work.pojo.TextWhole;
import cn.itcast.work.pojo.User;
import org.apache.ibatis.annotations.*;


import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 查询映射 接口
 */

@Mapper
public interface UserMapper {
    /**
     * 用户的增删查改
     *
     * @param user
     */
    //注册 增
    @Insert("insert into users (role,userName, userPassword, phone, email,put,pic,motto) values (#{role},#{username}, " +
            "#{userpassword}, #{phone}, #{email},0,'girl.jpg','welcome to Echo Blog')")
    public int toAdd(User user);

    //是否有重复邮箱
    @Select("select 1 from users where phone = #{phone}")
    public Integer toSelectP(String phone);

    //是否有重复邮箱
    @Select("select 1 from users where email = #{email}")
    public Integer toSelectE(String email);

    //查询 登录验证
    @Select("select role,userName,userPassword,phone,email from users where userPassword = #{userPassword} and phone" +
            " = #{phone}")
    public User toSelectU(@Param("phone") String phone, @Param("userPassword") String userPassword);

    //    //查询 所有用户的登录信息
//    @Select("select phone,userPassword from users")
//    public List<User> getAllUsers();
    @Select("select * from users where phone=#{phone}")
    public User selectUserByPhone(String phone);

    //删除用户
    @Delete("delete from users where phone=#{phone}")
    public int toDelete(@Param("phone") String phone);

    //根据用户名 查询用户的phone
    @Select("select phone from users where userName=#{author}")
    public String selectPhone(@Param("author") String author);

    //删除头像
    @Delete("delete from picture where phone=#{phone}")
    public int deletePic(@Param("phone") String phone);

    //删除 motto
    @Delete("delete from motto where phone=#{phone}")
    public int deleteMotto(@Param("phone") String phone);

    //通过phone删除博客
    @Delete("delete from text where phone=#{phone}")
    public int deleteTextP(@Param("phone") String phone);

    //form 查询信息
    @Select("select users.userName, users.userPassword,motto.content from users inner join motto on users.phone=" +
            "motto.phone where users.phone=#{phone} and motto.phone=#{phone}")
    public CUser selectInf(@Param("phone") String phone);

    //修改信息
    @Update("update users set userName=#{username}, userPassword=#{password} where phone=#{phone}")
    public int updateUser(@Param("username") String username, @Param("password") String password, @Param("phone") String phone);

    //修改个签
    @Update("update motto set content=#{motto} where phone=#{phone}")
    public int updateMotto(@Param("motto") String motto, @Param("phone") String phone);

    //查询个签
    @Select("select content from motto where phone =#{phone}")
    public String selectMotto(@Param("phone") String phone);

    //插入个签
    @Insert("insert into motto (phone, content) values(#{phone}, #{motto})")
    public int insertMotto(@Param("phone") String phone, @Param("motto") String motto);

    //上传图片
    @Insert("insert into picture (phone, img_path) values (#{phone}, #{path})")
    public int InsertPic(@Param("phone") String phone, @Param("path") String path);

    //修改图片
    @Update("update picture set img_path =#{path} where phone =#{phone}")
    public int UpdatePic(@Param("phone") String phone, @Param("path") String path);

    //查询图片
    @Select("select img_path from picture where phone =#{phone}")
    public String SelectPic(@Param("phone") String phone);


    /**
     * 博客的增删查改
     */
    @Insert("insert into text (phone, title, subtitle, content, createdate, cid, mid,status) values ( #{text.phone}, #{text.title}," +
            " #{text.subtitle}, #{text.content}, #{text.createdate}, #{text.cid}, #{text.mid},0)")
    public void insertText(@Param("text") TextWhole text);

    //根据用户 时间 查询博客
    @Select("select phone, title, subtitle, content, createdate, cid, mid,status from text where title =#{title}")
    public TextWhole selectText(@Param("title") String title);

    //查询用户所有的博客
    @Select("select title, createdate, subtitle, phone,status from text where phone =#{phone}")
    public List<TextWhole> selectAllText(@Param("phone") String phone);

    //通过tilte删除博客
    @Delete("delete from text where title=#{title}")
    public int deleteText(@Param("title") String title);

    //获取id
    @Select("select id from text where title=#{title}")
    public int selectId(@Param("title") String title);

    //通过id更新数据
    @Update("update text set title=#{text.title}, subtitle=#{text.subtitle}, content=#{text.content}," +
            "createdate=#{text.createdate}, cid=#{text.cid}, mid=#{text.mid},status=0,text.put=0 where id =#{id}")
    public int update(@Param("text") TextWhole textWhole, @Param("id") int id);

    //查询用户名
    @Select("select userName from users where phone =#{phone}")
    public String selectUserName(String phone);

    //查询用户名
    @Select("select mname, cname from aCategory join text on aCategory.id=text.mid" +
            " join category2 on category2.id= text.cid where title =#{title}")
    public InfoText selectTextInf(String title);

    /**
     * 文章被更新 首页表更新文章title
     */
    @Update("update home_page set title=#{title} where phone=#{phone}")
    public int updateHome_page(@Param("title") String title, @Param("phone") String phone);

    /**
     * 用户信息被修改 home_user 需要更新用户名
     */
    @Update("update home_user set name=#{name} where phone=#{phone}")
    public int updateHome_user(@Param("name") String name, @Param("phone") String phone);

    /**
     * 查询用户信息
     *
     * @param phone
     * @return
     */
    @Select("select * from users where phone=#{phone}")
    User selectUser(String phone);

    /**
     * 查询已审核的文章
     *
     * @param phone
     * @return
     */
    @Select("select * from text where status=1 and phone=#{phone}")
    List<TextWhole> toSeeStatus(String phone);
}
